import { connect } from 'dva'

import { systemModuleGetAllApi } from '@/api/app'
import AekTable from '@/components/AekTable'
import PageLayout from '@/components/PageLayout'
import SearchForm from '@/components/SearchForm'
import { namespace } from '@/models/sys/systemFeature'
import { getPagination } from '@/utils/antdHelper'
import { getBasicFn } from '@/utils/dvaUtil/index'

/** 系统 - 功能列表 */
const Index = ({ modelState, loading }) => {
  const { pageList, pagination, searchParams } = modelState
  const { toAction, getLoading } = getBasicFn({ namespace, loading })

  /** @type {import("@/components/SearchForm").SearchFormProps} */
  const searchProps = {
    searchParams,
    formData: [
      {
        field: 'moduleId',
        label: '系统',
        component: {
          name: 'AekSelect',
          props: {
            api: systemModuleGetAllApi,
            fieldNames: {
              value: 'moduleId',
              label: 'moduleName',
            },
          },
        },
      },
      {
        field: 'featureName',
        label: '功能名称',
        component: {
          name: 'Input',
        },
      },
    ],
    onSearch: values => {
      toAction({ searchParams: values })
      toAction({ pagination: { current: 1 } }, true)
      toAction('getPageList')
    },
  }

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: namespace,
    rowKey: 'featureId',
    loading: getLoading('getPageList'),
    dataSource: pageList,
    pagination: getPagination(pagination, (current, pageSize) => {
      toAction('getPageList', { current, pageSize })
    }),
    columns: [
      { dataIndex: 'moduleName', title: '系统' },
      { dataIndex: 'featureName', title: '功能名称' },
      {
        dataIndex: 'systemFeatureOperation',
        title: '界面权限',
        render: text => text.map(i => <div key={i.operationId}>{i.operationName}</div>),
      },
    ],
  }

  return (
    <PageLayout>
      <SearchForm {...searchProps} />
      <AekTable {...tableProps} />
    </PageLayout>
  )
}

export default connect(store => ({
  modelState: store[namespace],
  loading: store.loading,
}))(Index)
